#!/bin/bash

# 前端问题诊断脚本

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

print_header() {
    echo ""
    echo -e "${BLUE}========================================${NC}"
    echo -e "${BLUE}$1${NC}"
    echo -e "${BLUE}========================================${NC}"
    echo ""
}

# 主函数
main() {
    print_header "🔍 前端问题诊断"
    
    # 1. 检查 Node.js
    print_info "1. 检查 Node.js..."
    if command -v node &> /dev/null; then
        node_version=$(node --version)
        print_success "Node.js 版本: $node_version"
        
        # 检查版本是否符合要求
        major_version=$(echo $node_version | cut -d'.' -f1 | tr -d 'v')
        if [ "$major_version" -lt 16 ]; then
            print_warning "Node.js 版本过低，建议使用 16.x 或更高版本"
        fi
    else
        print_error "Node.js 未安装"
    fi
    
    echo ""
    
    # 2. 检查 npm
    print_info "2. 检查 npm..."
    if command -v npm &> /dev/null; then
        npm_version=$(npm --version)
        print_success "npm 版本: $npm_version"
    else
        print_error "npm 未安装"
    fi
    
    echo ""
    
    # 3. 检查依赖
    print_info "3. 检查依赖安装..."
    if [ -d "node_modules" ]; then
        module_count=$(ls -1 node_modules | wc -l)
        print_success "依赖已安装 ($module_count 个包)"
        
        # 检查关键依赖
        print_info "检查关键依赖..."
        for pkg in vue vue-router pinia axios element-plus vite; do
            if [ -d "node_modules/$pkg" ]; then
                print_success "  $pkg ✓"
            else
                print_error "  $pkg ✗"
            fi
        done
    else
        print_error "依赖未安装"
        print_info "运行: npm install"
    fi
    
    echo ""
    
    # 4. 检查配置文件
    print_info "4. 检查配置文件..."
    for file in package.json vite.config.js index.html; do
        if [ -f "$file" ]; then
            print_success "  $file ✓"
        else
            print_error "  $file ✗"
        fi
    done
    
    echo ""
    
    # 5. 检查源文件
    print_info "5. 检查源文件..."
    for file in src/main.js src/App.vue src/router/index.js; do
        if [ -f "$file" ]; then
            print_success "  $file ✓"
        else
            print_error "  $file ✗"
        fi
    done
    
    echo ""
    
    # 6. 检查端口占用
    print_info "6. 检查端口 5173..."
    if lsof -i :5173 > /dev/null 2>&1; then
        print_warning "端口 5173 被占用"
        print_info "占用进程:"
        lsof -i :5173 | grep LISTEN
    else
        print_success "端口 5173 可用"
    fi
    
    echo ""
    
    # 7. 检查后端服务
    print_info "7. 检查后端服务 (localhost:8000)..."
    if curl -s http://localhost:8000 > /dev/null 2>&1; then
        print_success "后端服务正在运行"
    else
        print_warning "后端服务未运行"
        print_info "启动命令: cd ../ai-agent && uvicorn app.main:app --reload"
    fi
    
    echo ""
    
    # 8. 检查 npm 漏洞
    print_info "8. 检查 npm 安全漏洞..."
    if [ -d "node_modules" ]; then
        npm audit --production > /tmp/npm_audit.txt 2>&1
        if grep -q "found 0 vulnerabilities" /tmp/npm_audit.txt; then
            print_success "生产依赖无漏洞"
        else
            print_warning "发现一些漏洞（可能是开发依赖）"
            print_info "运行 'npm audit' 查看详情"
        fi
        rm -f /tmp/npm_audit.txt
    fi
    
    echo ""
    
    # 9. 尝试启动（测试模式）
    print_info "9. 测试启动..."
    print_info "尝试启动开发服务器（5秒后自动停止）..."
    
    # 创建临时脚本
    cat > /tmp/test_start.sh << 'SCRIPT'
#!/bin/bash
npm run dev &
PID=$!
sleep 5
kill $PID 2>/dev/null
wait $PID 2>/dev/null
SCRIPT
    
    chmod +x /tmp/test_start.sh
    if /tmp/test_start.sh > /tmp/vite_output.txt 2>&1; then
        if grep -q "Local:" /tmp/vite_output.txt; then
            print_success "开发服务器可以正常启动"
        else
            print_warning "启动可能有问题"
        fi
    else
        print_error "启动失败"
        print_info "错误信息:"
        tail -10 /tmp/vite_output.txt
    fi
    
    rm -f /tmp/test_start.sh /tmp/vite_output.txt
    
    echo ""
    
    # 10. 总结
    print_header "📋 诊断总结"
    
    print_info "如果一切正常，运行以下命令启动:"
    echo "  npm run dev"
    echo ""
    print_info "或使用启动脚本:"
    echo "  ./start.sh"
    echo ""
    print_info "如果遇到问题，请查看:"
    echo "  • TROUBLESHOOTING.md - 故障排除指南"
    echo "  • README.md - 项目文档"
    echo ""
    print_info "常见解决方案:"
    echo "  • 依赖问题: rm -rf node_modules package-lock.json && npm install"
    echo "  • 端口占用: lsof -i :5173 && kill -9 <PID>"
    echo "  • 缓存问题: rm -rf node_modules/.vite"
    echo ""
}

# 执行主函数
main

